package com.sqkj.controller;

import com.sqkj.entity.User;
import com.sqkj.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {
    @Resource
    UserService service;
    @RequestMapping("/findAll")
    public Map<String,Object> findAll(){
        Map<String,Object> map = new HashMap<String,Object>();
        List<User> users = service.findAll();
        map.put("userInfoList", users);
        return map;
    }

    @RequestMapping("/findById")
    public Map<String,Object> findById(@RequestParam int id){
        Map<String,Object> map = new HashMap<String,Object>();
         map.put("userInfo",service.findById(id));
          return map;
    }
    @RequestMapping("/deleteById")
    public Map<String,Object> deleteById(int id){
        Map<String,Object> map = new HashMap<String,Object>();
        Boolean isSuccess = service.deleteById(id);
        map.put("Success", isSuccess);
        return map;
    }
    @RequestMapping("/upDate")
    public Map<String,Object> upDate(User user){
        Map<String,Object> map = new HashMap<String,Object>();
        Boolean isSuccess = service.upDate(user);
        map.put("Success", isSuccess);
        return map;
    }
    @RequestMapping("/like")
    public Map<String,Object> Like(@RequestParam String str){
        String b = "%" + str + "%";
        Map<String,Object> map = new HashMap<String,Object>();
        List<User> list = new ArrayList<User>();
        list = service.like(b);
         if(list!=null && list.size()>0){
             map.put("UserInfoList", list );
             return map;
         }else {
             map.put("UserInfoList", null);
             return map;
         }
    }
    @RequestMapping("/add")
    public Map<String,Object> addIdCard(User user){
        Map<String, Object> map = new HashMap<String, Object>();
         String userIcon = user.getUserIcon();
        String type = userIcon.indexOf(".") != -1 ? userIcon.substring(userIcon.lastIndexOf(".") + 1, userIcon.length()) : null;
        if (type != null && !"".equals(type)) {
            if ("GIF".equals(type.toUpperCase())||"PNG".equals(type.toUpperCase())||"JPG".equals(type.toUpperCase()) ||"JPEG".equals(type.toUpperCase())) {
                String userIcon2 = userIcon.substring(userIcon.lastIndexOf("/")+1);     //得到图片名
                String userIcon3 = "http://localhost:8080/Path2/UserIcon/" + userIcon2;
                user.setUserIcon(userIcon3);
                return addUser(user);
            }else {
                map.put("myCard", "只支持图片格式");
                return map;

            }
        } else {
            map.put("myCard", "文件类型为空");
            return map;
        }

     }

    public Map<String, Object> addUser(User user){
        Map<String, Object> map = new HashMap<String, Object>();
        if(user !=null && !"".equals(user.getOpenId() ) && !"".equals(user.getWxName() ) &&  !"".equals(user.getUserName() )&& !"".equals(user.getAddress()) &&!"".equals(user.getCNname()) &&!"".equals(user.getEmail()) &&!"".equals(user.getFax()) &&!"".equals(user.getJob()) &&!"".equals(user.getPhone()) &&!"".equals(user.getUSname() )){
            service.add(user);

            map.put("myCard", user.getId() );
            return map;
        }else {
            map.put("myCard", null);
            return map;
        }
    }
     @RequestMapping("/findByName2")
    public Map<String, Object> findByName2(String UserName) {
         Map<String, Object> map = new HashMap<String, Object>();
         Boolean isExist = false;
         if (isExist = service.findByName2(UserName)) {
             map.put("isExist", isExist);
             map.put("userList", service.findByName(UserName));
             return map;
         } else {
             map.put("isExist", isExist);
             return map;
         }

    }
    @RequestMapping("/findByWxName")
    public Map<String, Object> findByWxName(String wxName) {
        Map<String, Object> map = new HashMap<>();
        User user = service.findByWxName(wxName);
        map.put("user", user);
        return map;
    }
    @RequestMapping("/findByOpenId")
    public Map<String, Object> findByOpenId(String openId) {
        Map<String, Object> map = new HashMap<>();
        User user = service.findByOpenId(openId);
        map.put("user", user);
        return map;
    }

    @RequestMapping("/isExistUser")
    public Boolean isExistUser(String wxName) {
        Boolean isExist = service.isExistUser(wxName);
        return isExist;
    }
    @RequestMapping("/isExistUser2")
    public Boolean isExistUser2(String openId) {
        Boolean isExist = service.isExistUser2(openId);
        return isExist;
    }
}